package com.study.main.dao;

import com.study.common.BaseDao.BaseDaoImpl;
import com.study.common.entity.LoginException;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.main.entity.Information;
import com.study.main.vo.UserInfo;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Create by ldr.
 * @create 2018-08-30 14:29
 * @Description：
 */
@Repository(value = "mainDao")
public class MainDaoImpl extends BaseDaoImpl implements MainDao {

    @Override
    public void deleteByIds(int[] ids) {
        Session s = getSession();
        String hql = "";
        for (int i = 0; i < ids.length; i++) {
            if (i == 0) {
                hql = "infoId =" + ids[i];
            } else {
                hql = hql + "or infoId = " + ids[i];
            }
        }
        Query q = s.createQuery("delete from Information where " + hql);
        q.executeUpdate();
    }

    @Override
    public List<UserInfo> findPageByQuery(int pageNo, int pageSize, Map<String, Object> map) {
        List<UserInfo> result = null;
        String hql = "SELECT new com.study.main.vo.UserInfo(i.userId,i.infoId,u.chineseName,u.telNumber,u.gitlabNumber,u.email,i.credit,i.grade) " +
                "from User as u,Information as i where u.userId = i.userId and ";
        try {
            hql += " 1=1 and";
            Session s = getSession();
            if (!map.isEmpty()) {
                Iterator<String> it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    if (map.get(key) != null && !map.get(key).equals("")) {
                        String a = (String)map.get(key);
                        if(!a.contains("%")){
                        hql = hql + " " + key + " like " + "'%" + map.get(key) + "%'" + " and ";
                    }else{
                            hql = hql + "  1=2 and ";
                        }
                }
                }
            }
            hql += " 2=2";
            System.out.println(hql);
            Query query = s.createQuery(hql);
            query.setFirstResult((pageNo - 1) * pageSize);
            query.setMaxResults(pageSize);
            result = query.list();
        } catch (HibernateException e) {
            throw new LoginException(ExceptionInfo.SELECT_ERROR);
        }
        return result;
    }

    @Override
    public Long getRows(Map<String, Object> map) {
        String hql = "SELECT count(u.userId) " +
                "from User as u,Information as i where u.userId = i.userId and ";
        hql += " 1=1 and";
        Session s = getSession();
        if (!map.isEmpty()) {
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                if (map.get(key) != null && !map.get(key).equals("")) {
                String a = (String)map.get(key);
                if(!a.contains("%")){
                    hql = hql + " " + key + " like " + "'%" + map.get(key) + "%'" + " and ";
                }else{
                    hql = hql + " 1=2 and ";
                }
                }
            }
        }
        hql += " 2=2";
        System.out.println(hql);
        Query q = s.createQuery(hql);
        return (Long) q.uniqueResult();
    }

    @Override
    public void updateIdentity(int identityId, int userId) {
        Session s = getSession();
        String hql = "update User set identityId = ? where userId = ?";
        Query q = s.createQuery(hql);
        q.setParameter(0, identityId);
        q.setParameter(1, userId);
        q.executeUpdate();
    }

    @Override
    public List<Information> findByUserId(int userId) {
        Session s = getSession();
        String hql = " FROM Information WHERE userId = ?";
        Query q = s.createQuery(hql);
        q.setParameter(0, userId);
        return q.list();
    }

    public UserInfo findByInfoId(int infoId) {
        Session s = getSession();
        String hql = "SELECT new com.study.main.vo.UserInfo(i.userId,i.infoId,u.chineseName,u.telNumber,u.gitlabNumber,u.email,i.credit,i.grade) " +
                "from User u,Information i where u.userId = i.userId and i.infoId = ?";
        Query q = s.createQuery(hql);
        q.setParameter(0, infoId);
        return (UserInfo) q.uniqueResult();
    }

    @Override
    public void updateUser(User user) {
        Session s = getSession();
        String hql = "update User set gitlabNumber = ?,telNumber = ? where userId = ?";
        Query q = s.createQuery(hql);
        q.setParameter(0, user.getGitlabNumber());
        q.setParameter(1, user.getTelNumber());
        q.setParameter(2, user.getUserId());
        q.executeUpdate();
    }

    @Override
    public void updateInformation(Information information) {
        Session s = getSession();
        String hql = "update Information set credit = ?,grade = ? where infoId = ?";
        Query q = s.createQuery(hql);
        q.setParameter(0, information.getCredit());
        q.setParameter(1, information.getGrade());
        q.setParameter(2, information.getInfoId());
        q.executeUpdate();
    }

    @Override
    public List<User> findPageBy(Map<String, Object> map) {
        String hql = "from User as u where ";
        hql += " 1=1 and";
        Session s = getSession();
        if (!map.isEmpty()) {
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                if (map.get(key) != null && !map.get(key).equals("")) {
                    hql = hql + " " + key + " like " + "'%" + map.get(key) + "%'" + " and ";
                }
            }
        }
        hql += " 2=2";
        System.out.println(hql);
        Query q = s.createQuery(hql);
        return (List<User>) q.list();
    }
}
